<template>
	<!-- 修改户籍 -->
	<view class="building-add">
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">户号<text>*</text></view>
				<view class="item-right"><input placeholder="请输入户号" placeholder-style="color:#999;font-size:26rpx" v-model="domicile.familyCode"/></view>
			</view>
			<view class="add-item">
				<view class="item-left">户籍类型<text>*</text></view>
				<view class="item-right" @click="dictListShow=true">{{ domicile.type }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">户主姓名<text>*</text></view>
				<navigator class="item-right" url="../../Public/People" hover-class="none">{{ domicile.userName }}</navigator>
			</view>
			<view class="add-item">
				<view class="item-left">房屋<text>*</text></view>
				<view class="item-right" @click="houseShow=true">{{ domicile.house }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">户籍详址<text>*</text></view>
				<view class="item-right" @click="chooseAddress">{{ domicile.address }}</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 户籍类型 -->
		<u-select v-model="dictListShow" :list="dictList" @confirm="confirmType"></u-select>
		<!-- 房屋 -->
		<u-select v-model="houseShow" :list="houseList" @confirm="confirmHouse"></u-select>
		
	</view>
</template>

<script>
	export default{
		data() {
			return{
				// 汉字
				domicile:{
					familyCode:'',
					type:'请选择',// 户籍类型
					address:'请选择', // 户籍地址
					longitude:'',
					latitude:'',
					house:'请选择',
					userName:'请选择'
				},
				// 对应数字
				params:{
					type:'',// 户籍类型
					houseId:'',
					userId:'',
					id:''// 修改时的ID
				},
				dictList:[],
				dictListShow:false,
				houseShow:false,
				houseList:[]
			}
		},
		onLoad() {
			let info = JSON.parse(uni.getStorageSync('info'))
			console.log(info)
			this.params.id = info.id
			this.domicile.userName = info.familyHolderName
			this.params.userId = info.familyHolderId
			this.domicile.familyCode = info.familyCode
			this.domicile.address = info.familyAddress
			this.domicile.type = info.type
			this.params.houseId = info.houseId
			this.params.type = info.familyType
		},
		mounted() {
			this.getDictionaries()
			this.getlistHouse()
			this.getHouseById()
		},
		onShow() {
			if(!uni.getStorageSync('people')) return
			let houseUser = uni.getStorageSync('people')
			this.domicile.userName = houseUser.name
			this.params.userId = houseUser.id
		},
		methods:{
			// 获取房屋
			getHouseById() {
				let params = {
					houseId:this.params.houseId
				}
				this.$u.api.getHouseById(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							this.domicile.house = res.result.fullName
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 户籍类型
			getDictionaries() {
				let params = {
					dictCode:'family_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.dictList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 确认户籍
			confirmType(e) {
				// console.log(e)
				this.domicile.type = e[0].label
				this.params.type = e[0].value
			},
			// 房屋
			getlistHouse() {
				this.$u.api.getlistHouse()
					.then(res => {
						// console.log(res)
						res.result.forEach(item => {
							this.houseList.push({
								value:item.id,
								label:item.fullName
							})
						})
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 确认房屋
			confirmHouse(e) {
				// console.log(e)
				this.domicile.house = e[0].label
				this.params.houseId = e[0].value
			},
			// 选择地址
			chooseAddress() {
				let self = this
				uni.chooseLocation({
					success(res) {
						// console.log(res)
						self.domicile.address = res.address
						self.domicile.longitude = res.longitude
						self.domicile.latitude = res.latitude
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取地址失败'
						})
					}
				})
			},
			// 提交 修改户籍
			handleSubmit() {
				if(this.domicile.familyCode == '') {
					return this.$refs.uToast.show({
						title: '请输入户号'
					})
				}
				if(this.params.type == '') {
					return this.$refs.uToast.show({
						title: '请选择户籍类型'
					})
				}
				if(this.params.userId == '') {
					return this.$refs.uToast.show({
						title: '请选择户主'
					})
				}
				if(this.params.houseId == '') {
					return this.$refs.uToast.show({
						title: '请选择房屋'
					})
				}
				if(this.domicile.address == '请选择') {
					return this.$refs.uToast.show({
						title: '请选择地址'
					})
				}
				let params = {
					familyType:this.params.type,
					familyCode:this.domicile.familyCode,
					familyAddress:this.domicile.address,
					familyHolderId:this.params.userId,
					houseId:this.params.houseId,
					id:this.params.id
				}
				console.log(params)
				this.$u.api.editFamily(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.removeStorageSync('people')
							this.params.type = ''
							this.params.houseId = ''
							this.params.userId = ''
							this.domicile.familyCode = ''
							this.domicile.type = '请选择'
							this.domicile.address = '请选择'
							this.domicile.house = '请选择'
							this.domicile.userName = '请选择'
							uni.showToast({
								title:'修改成功',
								icon:'success'
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'修改失败',
							icon:'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #999;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				image{
					width: 50%;
					height: 50%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text-align: left;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					flex: 6;
					font-size: 27rpx;
					color: #999;
					text-align: right;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #cecece;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
